package com.wave.lib.pull;

import android.content.Context;
import android.graphics.Typeface;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author Wave.Zhang
 * @version 1.0
 * @data 15/6/11
 */
public class SystemUtils {

    public static Typeface mTypeface;

    public static Typeface getRenMaiTypeface(Context mContext){
        if(mTypeface == null){
            mTypeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/FZLTXH.TTF");
        }
        return mTypeface;
    }

    public static String getRenMaiCacheDir(Context m){
        File file = new File(m.getFilesDir()+ "/renmaicaches");
        if(!file.exists()){
            file.mkdirs();
        }
        return file.getPath();
    }

    public static boolean saveCacheFile(Context m,String fileName,String content){
        boolean flag = false;
        FileOutputStream fos = null;
        try{
            File file = new File(getRenMaiCacheDir(m)+ "/" + fileName);
            if(!file.exists()){
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            fos.write(content.getBytes());
            //System.out.println("  创建成功。。。。。 ");
            //fos = m.openFileOutput(fileName, m.MODE_PRIVATE);
            //fos.write(content.getBytes());
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
            return false;
        }finally {
            try{
                if(fos != null)fos.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }

    public static String readCacheFile(Context m,String fileName){
        FileInputStream fis = null;
        StringBuffer sb = new StringBuffer();
        try{
            File file = new File(getRenMaiCacheDir(m)+ "/" + fileName);
            if(!file.exists()){
                return null;
            }

            fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int len = 0;
            //将读取后的数据放置在内存中
            while ((len = fis.read(b)) != -1) {
                sb.append(new String(b, 0, len));
            }
        }catch (Exception e){
            return "";
        }finally {
            try {
                if(fis != null)fis.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        //返回内存中存储的数据
        return sb.toString();
    }
}
